package com.xq.shop.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xq.shop.config.FileConfig;
import com.xq.shop.config.Redis;
import com.xq.shop.dao.domain.entity.Banner;
import com.xq.shop.dao.domain.entity.MyFile;
import com.xq.shop.dao.mapper.BannerMapper;
import com.xq.shop.dto.EditBannerDto;
import com.xq.shop.dto.IntegerDto;
import com.xq.shop.dto.QueryDto;
import com.xq.shop.service.IBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.shop.service.IFileService;
import com.xq.shop.vo.BannerVo;
import enums.DataStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 轮播表 服务实现类
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-22
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {

    private final IFileService fileService;

    @Autowired
    public BannerServiceImpl(IFileService fileService) {
        this.fileService = fileService;
    }
    @Resource
    private FileConfig fileConfig;
    @Resource
    private Redis redis;

    @Override
    public List<BannerVo> queryBanner(QueryDto dto) {
        LambdaQueryWrapper<Banner> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Banner::getStatus, DataStatus.NORMAL.getCode());
        if (!StringUtils.isEmpty(dto.getContent())) {
            wrapper.like(Banner::getTitle, dto.getContent());
        }
        List<Banner> bannerList = list(wrapper);
        if (bannerList.isEmpty()) {
            return new ArrayList<>();
        } else {
            return beanToVo(bannerList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editBanner(EditBannerDto dto, Integer userId) {
        Banner banner = new Banner();
        if (dto.getId().equals(0)) {
            banner.setCreateUser(userId);
            BeanUtils.copyProperties(dto, banner);
            save(banner);
        } else {
            banner = getById(dto.getId());
            BeanUtils.copyProperties(dto, banner);
            banner.setUpdateUser(userId);
            updateById(banner);
        }
        redis.remove("banner");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeBanner(IntegerDto dto, Integer id) {
        Banner banner = getById(dto.getValue());
        if (Objects.nonNull(banner)) {
            banner.setStatus(DataStatus.INVALID.getCode());
            banner.setUpdateUser(id);
            updateById(banner);
        }
        redis.remove("banner");
    }

    private List<BannerVo> beanToVo(List<Banner> bannerList) {
        List<BannerVo> voList = new ArrayList<>();
        bannerList.forEach(banner -> {
            BannerVo vo = new BannerVo();
            BeanUtils.copyProperties(banner, vo);
            MyFile file = fileService.getById(banner.getImageId());
            vo.setImageUrl(fileConfig.montageUrl(file.getUrl()));
            voList.add(vo);
        });
        return voList;
    }
}
